package com.xmb.common.service.impl;

import com.xmb.common.constants.ExchangeTypeEnum;
import com.xmb.common.constants.OrderRewardSendEnum;
import com.xmb.common.constants.PromotionEnum;
import com.xmb.common.constants.RewardTypeEnum;
import com.xmb.common.dao.XmbOrderRewardRecordMapper;
import com.xmb.common.dao.XmbOrderRewardSettingMapper;
import com.xmb.common.entity.*;
import com.xmb.common.remote.entity.OrderRewardParam;
import com.xmb.common.remote.entity.XcxUser;
import com.xmb.common.remote.service.UserService;
import com.xmb.common.service.OrderRewardSettingService;
import com.xmb.common.service.UserBalanceService;
import com.xmb.common.service.UserBonusService;
import com.xmb.common.service.UserScoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * Created by lyndon on 2019/4/10.
 */
@Service
public class OrderRewardSettingServiceImpl implements OrderRewardSettingService {

    @Resource
    private XmbOrderRewardSettingMapper orderRewardSettingMapper;

    @Resource
    private XmbOrderRewardRecordMapper xmbOrderRewardRecordMapper;

    @Resource
    private UserService userService;

    @Resource
    private UserBonusService bonusService;

    @Resource
    private UserBalanceService balanceService;

    @Resource
    private UserScoreService scoreService;


    @Override
    public int saveXmbOrderRewardSetting(XmbOrderRewardSetting xmbOrderRewardSetting) {
        return orderRewardSettingMapper.insertSelective(xmbOrderRewardSetting);
    }

    @Override
    public int updateXmbOrderRewardSetting(XmbOrderRewardSetting xmbOrderRewardSetting) {
        return orderRewardSettingMapper.updateByPrimaryKeySelective(xmbOrderRewardSetting);
    }

    @Override
    public XmbOrderRewardSetting findXmbOrderRewardSetting(Long senceId, String senceType) {
        XmbOrderRewardSetting orderRewardSetting = new XmbOrderRewardSetting();
        orderRewardSetting.setSenceId(senceId);
        orderRewardSetting.setSenceType(senceType);
        orderRewardSetting.setIsDelete(Boolean.FALSE);
        return orderRewardSettingMapper.findXmbOrderRewardSetting(orderRewardSetting);
    }

    @Override
    public int saveOrderRewardRecord(OrderRewardParam orderRewardParam) {
        XmbOrderRewardSetting orderRewardSetting
                = this.findXmbOrderRewardSetting(orderRewardParam.getSenceId(),orderRewardParam.getSenceType());
        if (orderRewardSetting != null) {
            XmbOrderRewardRecord orderRewardRecord = new XmbOrderRewardRecord();
            XcxUser xcxUser = new XcxUser();
            xcxUser.setId(orderRewardParam.getUserId());
            XcxUser orderUser = userService.selectOneUser(xcxUser);
            if (orderUser != null) {
                orderRewardRecord.setUserId(orderUser.getId());
                orderRewardRecord.setUserNo(orderUser.getUserNo());
                orderRewardRecord.setNickName(orderUser.getNickName());
                orderRewardRecord.setRewardType(orderRewardSetting.getPromotionRewardType());
                orderRewardRecord.setSenceId(orderRewardSetting.getSenceId());
                orderRewardRecord.setSenceType(orderRewardSetting.getSenceType());
                orderRewardRecord.setSupplierId(orderUser.getSupplierId());
                orderRewardRecord.setOrderId(orderRewardParam.getOrderId());
                orderRewardRecord.setExchangeRemark(orderRewardParam.getExchangeRemark());

                // 获取a分销比例
                if (orderUser.getaHigherId() != null && orderUser.getaHigherId() > 0) {
                    XcxUser a = new XcxUser();
                    a.setId(orderUser.getaHigherId());
                    a = userService.selectOneUser(a);
                    if (a != null) {
                        orderRewardRecord.setPromotionAId(a.getId());
                        orderRewardRecord.setPromotionANickName(a.getNickName());
                        orderRewardRecord.setPromotionANo(a.getUserNo());
                        orderRewardRecord.setPromotionAReward(orderRewardSetting.getPromotionAReward());
                    }
                }


                // 获取b分销比例
                if (orderUser.getbHigherId() != null && orderUser.getbHigherId() > 0) {
                    XcxUser b = new XcxUser();
                    b.setId(orderUser.getbHigherId());
                    b = userService.selectOneUser(b);
                    if (b != null) {
                        orderRewardRecord.setPromotionBId(b.getId());
                        orderRewardRecord.setPromotionBNickName(b.getNickName());
                        orderRewardRecord.setPromotionBNo(b.getUserNo());
                        orderRewardRecord.setPromotionBReward(orderRewardSetting.getPromotionBReward());
                    }
                }


                // 获取c分销比例
                if (orderUser.getcHigherId() != null && orderUser.getcHigherId() > 0) {
                    XcxUser c = new XcxUser();
                    c.setId(orderUser.getcHigherId());
                    c = userService.selectOneUser(c);
                    if (c != null) {
                        orderRewardRecord.setPromotionCId(c.getId());
                        orderRewardRecord.setPromotionCNickName(c.getNickName());
                        orderRewardRecord.setPromotionCNo(c.getUserNo());
                        orderRewardRecord.setPromotionCReward(orderRewardSetting.getPromotionCReward());
                    }
                }


                // 获取w分销比例
                if (orderUser.getwHigherId() != null && orderUser.getwHigherId() > 0) {
                    XcxUser w = new XcxUser();
                    w.setId(orderUser.getwHigherId());
                    w = userService.selectOneUser(w);
                    if (w != null) {
                        orderRewardRecord.setPromotionWId(w.getId());
                        orderRewardRecord.setPromotionWNickName(w.getNickName());
                        orderRewardRecord.setPromotionWNo(w.getUserNo());
                        orderRewardRecord.setPromotionWReward(orderRewardSetting.getPromotionWReward());
                    }
                }
            }
            return xmbOrderRewardRecordMapper.insertSelective(orderRewardRecord);
        }
        return 0;
    }

    @Override
    public int sendOrderReward(OrderRewardParam orderRewardParam) {
        XmbOrderRewardRecord orderRewardRecord = new XmbOrderRewardRecord();
        orderRewardRecord.setSenceId(orderRewardParam.getSenceId());
        orderRewardRecord.setSenceType(orderRewardParam.getSenceType());
        orderRewardRecord.setUserId(orderRewardParam.getUserId());
        orderRewardRecord.setIsDelete(Boolean.FALSE);
        orderRewardRecord.setProvideStatus(OrderRewardSendEnum.wait_provide.name());
        orderRewardRecord.setOrderId(orderRewardParam.getOrderId());
        XmbOrderRewardRecord sendRewardRecord
                = xmbOrderRewardRecordMapper.findXmbOrderRewardRecord(orderRewardRecord);
        if (sendRewardRecord != null) {
            String exchange = orderRewardRecord.getExchangeRemark() +"%s"+ "分销奖励";
            // 写入各个配置的流水
            // 奖金
            if (RewardTypeEnum.bonus.name().equals(sendRewardRecord.getRewardType())) {
                this.saveBonusBlotter(sendRewardRecord.getPromotionAId(), sendRewardRecord.getPromotionAReward(), sendRewardRecord, PromotionEnum.A);
                this.saveBonusBlotter(sendRewardRecord.getPromotionBId(),sendRewardRecord.getPromotionBReward(),sendRewardRecord,PromotionEnum.B);
                this.saveBonusBlotter(sendRewardRecord.getPromotionCId(),sendRewardRecord.getPromotionCReward(),sendRewardRecord,PromotionEnum.C);
                this.saveBonusBlotter(sendRewardRecord.getPromotionWId(),sendRewardRecord.getPromotionWReward(),sendRewardRecord,PromotionEnum.W);
            }

            // 积分
            if (RewardTypeEnum.score.name().equals(sendRewardRecord.getRewardType())) {
                this.saveScoreBlotter(sendRewardRecord.getPromotionAId(),sendRewardRecord.getPromotionAReward(),sendRewardRecord,PromotionEnum.A);
                this.saveScoreBlotter(sendRewardRecord.getPromotionBId(),sendRewardRecord.getPromotionBReward(),sendRewardRecord,PromotionEnum.B);
                this.saveScoreBlotter(sendRewardRecord.getPromotionCId(),sendRewardRecord.getPromotionCReward(),sendRewardRecord,PromotionEnum.C);
                this.saveScoreBlotter(sendRewardRecord.getPromotionWId(),sendRewardRecord.getPromotionWReward(),sendRewardRecord,PromotionEnum.W);
            }

            //余额
            if (RewardTypeEnum.balance.name().equals(sendRewardRecord.getRewardType())) {
                this.saveBalanceBlotter(sendRewardRecord.getPromotionAId(),sendRewardRecord.getPromotionAReward(),sendRewardRecord,PromotionEnum.A);
                this.saveBalanceBlotter(sendRewardRecord.getPromotionBId(),sendRewardRecord.getPromotionBReward(),sendRewardRecord,PromotionEnum.B);
                this.saveBalanceBlotter(sendRewardRecord.getPromotionCId(),sendRewardRecord.getPromotionCReward(),sendRewardRecord,PromotionEnum.C);
                this.saveBalanceBlotter(sendRewardRecord.getPromotionWId(),sendRewardRecord.getPromotionWReward(),sendRewardRecord,PromotionEnum.W);
            }
            sendRewardRecord.setProvideStatus(OrderRewardSendEnum.provided.name());
            sendRewardRecord.setProvideTime(LocalDateTime.now());
            xmbOrderRewardRecordMapper.updateByPrimaryKeySelective(sendRewardRecord);
        }
        return 0;
    }

    @Override
    public int deleteXmbOrderRewardSetting(Long id) {
        XmbOrderRewardSetting orderRewardSetting = orderRewardSettingMapper.selectByPrimaryKey(id);
        if (orderRewardSetting != null) {
            orderRewardSetting.setIsDelete(Boolean.TRUE);
        }
        return orderRewardSettingMapper.updateByPrimaryKeySelective(orderRewardSetting);
    }



    /**
     * 保存奖金流水
     * @param uid
     * @param exchangeAmount
     * @param orderRewardRecord
     */
    private void saveBonusBlotter(Long uid, BigDecimal exchangeAmount, XmbOrderRewardRecord orderRewardRecord, PromotionEnum promotionEnum) {
        if (exchangeAmount.compareTo(BigDecimal.ZERO) == 1
                && uid!= null
                && uid > 0) {
            XmbUserBonusBlotter bonusBlotter = new XmbUserBonusBlotter();
            bonusBlotter.setUserId(uid);
            bonusBlotter.setExchangeAmount(exchangeAmount);
            bonusBlotter.setTotalAmount(bonusService.getTotalBonus(uid).add(exchangeAmount));
            bonusBlotter.setTypeId(orderRewardRecord.getSenceId());
            bonusBlotter.setExchangeRemark(orderRewardRecord.getExchangeRemark());
            bonusBlotter.setExchangeType(ExchangeTypeEnum.order_reward.name());
            bonusBlotter.setSupplierId(orderRewardRecord.getSupplierId());
            bonusBlotter.setExchangeTitle(this.getExchangeTitle(orderRewardRecord.getExchangeRemark(),promotionEnum));
            bonusService.saveBonusBlotter(bonusBlotter);
        }
    }

    /**
     * 保存积分流水
     * @param uid
     * @param exchangeAmount
     * @param orderRewardRecord
     */
    private void saveScoreBlotter(Long uid,BigDecimal exchangeAmount,XmbOrderRewardRecord orderRewardRecord,PromotionEnum promotionEnum) {
        if (exchangeAmount.compareTo(BigDecimal.ZERO) == 1
                && uid!= null
                && uid > 0) {
            XmbUserScoreBlotter scoreBlotter = new XmbUserScoreBlotter();
            scoreBlotter.setUserId(uid);
            scoreBlotter.setExchangeAmount(exchangeAmount);
            scoreBlotter.setTotalAmount(scoreService.getTotalScore(uid).add(exchangeAmount));
            scoreBlotter.setTypeId(orderRewardRecord.getSenceId());
            scoreBlotter.setExchangeRemark(orderRewardRecord.getExchangeRemark());
            scoreBlotter.setExchangeType(ExchangeTypeEnum.order_reward.name());
            scoreBlotter.setSupplierId(orderRewardRecord.getSupplierId());
            scoreBlotter.setExchangeTitle(this.getExchangeTitle(orderRewardRecord.getExchangeRemark(),promotionEnum));
            scoreService.saveScoreBlotter(scoreBlotter);
        }
    }

    /**
     * 保存余额流水
     * @param uid
     * @param exchangeAmount
     * @param orderRewardRecord
     */
    private void saveBalanceBlotter(Long uid,BigDecimal exchangeAmount,XmbOrderRewardRecord orderRewardRecord,PromotionEnum promotionEnum) {
        if (exchangeAmount.compareTo(BigDecimal.ZERO) == 1
                && uid!= null
                && uid > 0) {
            XmbUserBalanceBlotter balanceBlotter = new XmbUserBalanceBlotter();
            balanceBlotter.setUserId(uid);
            balanceBlotter.setExchangeAmount(exchangeAmount);
            balanceBlotter.setTotalAmount(balanceService.getTotalBalance(uid).add(exchangeAmount));
            balanceBlotter.setTypeId(orderRewardRecord.getSenceId());
            balanceBlotter.setExchangeRemark(orderRewardRecord.getExchangeRemark());
            balanceBlotter.setExchangeType(ExchangeTypeEnum.order_reward.name());
            balanceBlotter.setSupplierId(orderRewardRecord.getSupplierId());
            balanceBlotter.setExchangeTitle(this.getExchangeTitle(orderRewardRecord.getExchangeRemark(),promotionEnum));
            balanceService.saveBalanceBlotter(balanceBlotter);
        }
    }

    private String getExchangeTitle(String exchangeRemark,PromotionEnum promotionEnum) {
        return exchangeRemark + promotionEnum.getDesc() + "分销奖励";
    }
}
